package com.campus.counseling.controller;

import com.campus.counseling.model.common.Result;
import com.campus.counseling.model.vo.MenuVO;
import com.campus.counseling.model.vo.RouterVO;
import com.campus.counseling.entity.SysMenu;
import com.campus.counseling.service.SysMenuService;
import com.campus.counseling.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/api/menu")
@RequiredArgsConstructor
public class MenuController {

    private static final Logger log = LoggerFactory.getLogger(MenuController.class);

    private final SysMenuService menuService;

    @ApiOperation("获取菜单树")
    @GetMapping("/tree")
    public Result<List<MenuVO>> getMenuTree() {
        log.info("开始获取菜单树");
        List<MenuVO> menuTree = menuService.getMenuTree();
        log.info("菜单树获取成功: {}", menuTree);
        return Result.success(menuTree);
    }

    @ApiOperation("获取用户菜单")
    @GetMapping("/user")
    public Result<List<RouterVO>> getUserMenus() {
        Long userId = SecurityUtils.getCurrentUserId();
        return Result.success(menuService.getUserRouters(userId));
    }

    @ApiOperation("添加菜单")
    @PostMapping
    public Result<Void> add(@RequestBody SysMenu menu) {
        log.info("添加菜单: {}", menu);
        menuService.save(menu);
        return Result.success();
    }

    @ApiOperation("更新菜单")
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody SysMenu menu) {
        log.info("更新菜单, id: {}, menu: {}", id, menu);
        menu.setId(id);
        menuService.updateById(menu);
        return Result.success();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("删除菜单, id: {}", id);
        menuService.removeById(id);
        return Result.success();
    }
}